/*
 * SteepTimer - A timer designed for tea fanatics
 * Copyright (C) 2011  Wesley Crossman
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "TeaIcon.h"
#include <QPainter>
#include <QSettings>
#include <QDebug>

TeaIcon *TeaIcon::m_instance = NULL;

TeaIcon::TeaIcon() {
}

TeaIcon *TeaIcon::instance() {
    if (!m_instance) m_instance = new TeaIcon;
    return m_instance;
}

QPixmap TeaIcon::generateIcon(const TeaIconSettings &settings) {
    QString string = settings.toString();
    if (pixmapCache.contains(string)) return pixmapCache[string];

    bool iscup = (settings.m_vessel < Gaiwan);
    QImage canvas(16, 16, QImage::Format_ARGB32);
    canvas.fill(QColor(0, 0, 0, 0).value());
    QPainter paint(&canvas);
    const char *vesselName[] = {"mug", "teacup", "jpteacup", "gaiwan", "kyusu", "teapot"};
    const char *colorName[] = {"NONE", "blue", "gray", "green"};
    const int NA = -1000;
    int offsets[6][6] = {//SteamX,Y,BagX,Y,DrinkX,Y
                           {    4,0,   4,7,     3,3}, //Mug
                           {    4,0,   4,9,     3,5}, //Teacup
                           {    5,0,   5,9,     4,5}, //JpTeacup
                           {NA,NA,NA,NA,NA,NA},       //Gaiwan
                           {NA,NA,NA,NA,NA,NA},       //Kyusu
                           {NA,NA,NA,NA,NA,NA},       //Teapot
                       };
    QColor colors[] = {
                         QColor(0,0,0,0),   //none
                         QColor(84,44,0),   //coffee
                         QColor(157,48,0),  //rooibos
                         QColor(133,61,0),  //black tea
                         QColor(216,114,0), //oolong tea
                         QColor(109,122,29),//green tea
                         QColor(182,199,91),//white tea
                      };

    //draw cup
    QString subfile;
    if (iscup) {
        subfile = colorName[settings.m_color];
    } else {
        subfile = settings.m_running ? "open" : "closed";
    }
    QString filename = QString(":/systray/systray_%1_%2.png").arg(vesselName[settings.m_vessel]).arg(subfile);
    paint.drawImage(0, 0, QImage(filename));

    //draw liquid
    if (iscup) {
        paint.save();
        paint.translate(offsets[settings.m_vessel][4], offsets[settings.m_vessel][5]);
        paint.setPen(colors[settings.m_type]);
        paint.fillRect(2, 0, 4, 3, colors[settings.m_type]);
        paint.drawLine(QPoint(0,1), QPoint(7,1));
        paint.restore();
    }

    //draw steam
    if (settings.m_running && iscup) {
        paint.save();
        paint.translate(offsets[settings.m_vessel][0], offsets[settings.m_vessel][1]);
        paint.setPen(QColor(0, 0, 0, 127));
        paint.drawLine(QPoint(0, 0), QPoint(0, 6));
        paint.drawLine(QPoint(2, 0), QPoint(2, 6));
        paint.drawLine(QPoint(4, 0), QPoint(4, 6));
        paint.restore();
    }

    //draw teabag
    if (settings.m_running && settings.m_teabag) {
        paint.save();
        paint.translate(offsets[settings.m_vessel][2], offsets[settings.m_vessel][3]);
        paint.fillRect(0, 0, 3, 3, Qt::white);
        paint.restore();
    }

    //save and return the pixmap
    paint.end();
    QPixmap pix = QPixmap::fromImage(canvas);
    pixmapCache[string] = pix;
    return pix;
}

QString TeaIconSettings::toString() const {
    return QString("%1-%2-%3-%4-%5").arg(m_vessel).arg(m_running).arg(m_color).arg(m_type).arg(m_teabag);
}

void TeaIconSettings::loadFromSettings() {
    QSettings settings;
    settings.beginGroup("settings");
    m_vessel = static_cast < TeaIcon::Vessel >(settings.value("vessel").toInt());
    m_running = false;
    m_color = static_cast < TeaIcon::CupColor >(settings.value("color").toInt());
    m_type = static_cast < TeaIcon::DrinkType >(settings.value("type").toInt());
    m_teabag = settings.value("teabag").toBool();
    settings.endGroup();
}

void TeaIconSettings::saveToSettings() {
    QSettings settings;
    settings.beginGroup("settings");
    settings.setValue("vessel", m_vessel);
    settings.setValue("color", m_color);
    settings.setValue("type", m_type);
    settings.setValue("teabag", m_teabag);
    settings.endGroup();
}
